/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farent.solutions.plugins.aribaweb;

import com.farent.solutions.plugins.aribaweb.options.AribaWebOptionsPanel;
import java.io.File;
import java.util.prefs.Preferences;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import org.netbeans.spi.project.ui.support.ProjectChooser;
import org.openide.WizardDescriptor;
import org.openide.WizardValidationException;
import org.openide.filesystems.FileUtil;
import org.openide.util.NbPreferences;

public class NBawProjectWizardPanelVisual extends JPanel implements DocumentListener
{

    public static final String PROP_PROJECT_NAME = "projectName";
    private NBawProjectWizardPanel panel;
    String awHome;

    public NBawProjectWizardPanelVisual( NBawProjectWizardPanel panel )
    {
        initComponents();
        this.panel = panel;
        // Register listener on the textFields to make the automatic updates
        projectNameTextField.getDocument().addDocumentListener( this );
        projectLocationTextField.getDocument().addDocumentListener( this );

        Preferences pref = NbPreferences.forModule(AribaWebOptionsPanel.class);
        awHome = pref.get(NBawConstants.AW_HOME, "");

        aribawebHomeTextField.setEditable( awHome.isEmpty() );
        aribawebHomeTextField.setEnabled( awHome.isEmpty() );
        aribawebHomeTextField.setText( awHome );
        changeAWhomeChkBx.setSelected( awHome.isEmpty() );
        awHomeButton.setEnabled( awHome.isEmpty() );
    }

    public String getProjectName()
    {
        return this.projectNameTextField.getText();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        projectNameLabel = new javax.swing.JLabel();
        projectNameTextField = new javax.swing.JTextField();
        projectLocationLabel = new javax.swing.JLabel();
        projectLocationTextField = new javax.swing.JTextField();
        browseButton = new javax.swing.JButton();
        createdFolderLabel = new javax.swing.JLabel();
        createdFolderTextField = new javax.swing.JTextField();
        projectLocationLabel1 = new javax.swing.JLabel();
        aribawebHomeTextField = new javax.swing.JTextField();
        awHomeButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        changeAWhomeChkBx = new javax.swing.JCheckBox();

        projectNameLabel.setLabelFor(projectNameTextField);
        org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, org.openide.util.NbBundle.getMessage(NBawProjectWizardPanelVisual.class, "NBawProjectWizardPanelVisual.projectNameLabel.text")); // NOI18N

        projectLocationLabel.setLabelFor(projectLocationTextField);
        org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, org.openide.util.NbBundle.getMessage(NBawProjectWizardPanelVisual.class, "NBawProjectWizardPanelVisual.projectLocationLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(NBawProjectWizardPanelVisual.class, "NBawProjectWizardPanelVisual.browseButton.text")); // NOI18N
        browseButton.setActionCommand(org.openide.util.NbBundle.getMessage(NBawProjectWizardPanelVisual.class, "NBawProjectWizardPanelVisual.browseButton.actionCommand")); // NOI18N
        browseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseButtonActionPerformed(evt);
            }
        });

        createdFolderLabel.setLabelFor(createdFolderTextField);
        org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, org.openide.util.NbBundle.getMessage(NBawProjectWizardPanelVisual.class, "NBawProjectWizardPanelVisual.createdFolderLabel.text")); // NOI18N

        createdFolderTextField.setEditable(false);

        projectLocationLabel1.setLabelFor(projectLocationTextField);
        org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel1, org.openide.util.NbBundle.getMessage(NBawProjectWizardPanelVisual.class, "NBawProjectWizardPanelVisual.projectLocationLabel1.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(awHomeButton, org.openide.util.NbBundle.getMessage(NBawProjectWizardPanelVisual.class, "NBawProjectWizardPanelVisual.awHomeButton.text")); // NOI18N
        awHomeButton.setActionCommand(org.openide.util.NbBundle.getMessage(NBawProjectWizardPanelVisual.class, "NBawProjectWizardPanelVisual.awHomeButton.actionCommand")); // NOI18N
        awHomeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                awHomeButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(changeAWhomeChkBx, org.openide.util.NbBundle.getMessage(NBawProjectWizardPanelVisual.class, "NBawProjectWizardPanelVisual.changeAWhomeChkBx.text")); // NOI18N
        changeAWhomeChkBx.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                changeAWhomeChkBxStateChanged(evt);
            }
        });
        changeAWhomeChkBx.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changeAWhomeChkBxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(projectNameLabel)
                            .addComponent(projectLocationLabel)
                            .addComponent(createdFolderLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(projectNameTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                            .addComponent(projectLocationTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                            .addComponent(createdFolderTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(browseButton))
                    .addComponent(changeAWhomeChkBx)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(projectLocationLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(aribawebHomeTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(awHomeButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(projectNameLabel)
                    .addComponent(projectNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(projectLocationLabel)
                    .addComponent(projectLocationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(browseButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(createdFolderLabel)
                    .addComponent(createdFolderTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addComponent(changeAWhomeChkBx)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(projectLocationLabel1)
                    .addComponent(aribawebHomeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(awHomeButton))
                .addContainerGap(120, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void browseButtonActionPerformed( java.awt.event.ActionEvent evt )//GEN-FIRST:event_browseButtonActionPerformed
    {//GEN-HEADEREND:event_browseButtonActionPerformed
        String command = evt.getActionCommand();
        if ( "BROWSE".equals( command ) )
        {
            JFileChooser chooser = new JFileChooser();
            FileUtil.preventFileChooserSymlinkTraversal( chooser, null );
            chooser.setDialogTitle( "Select Project Location" );
            chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
            String path = this.projectLocationTextField.getText();
            if ( path.length() > 0 )
            {
                File f = new File( path );
                if ( f.exists() )
                {
                    chooser.setSelectedFile( f );
                }
            }
            if ( JFileChooser.APPROVE_OPTION == chooser.showOpenDialog( this ) )
            {
                File projectDir = chooser.getSelectedFile();
                projectLocationTextField.setText( FileUtil.normalizeFile( projectDir ).getAbsolutePath() );
            }
            panel.fireChangeEvent();
        }

    }//GEN-LAST:event_browseButtonActionPerformed

    private void awHomeButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_awHomeButtonActionPerformed
    {//GEN-HEADEREND:event_awHomeButtonActionPerformed
        String command = evt.getActionCommand();
        if ( "SET_AW_HOME".equals( command ) )
        {
            JFileChooser chooser = new JFileChooser();
            FileUtil.preventFileChooserSymlinkTraversal( chooser, null );
            chooser.setDialogTitle( "Select AribaWeb Home" );
            chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
            String path = awHome;
            if ( path.length() > 0 )
            {
                File f = new File( path );
                if ( f.exists() )
                {
                    chooser.setSelectedFile( f );
                }
            }
            if ( JFileChooser.APPROVE_OPTION == chooser.showOpenDialog( this ) )
            {
                File projectDir = chooser.getSelectedFile();
                aribawebHomeTextField.setText( FileUtil.normalizeFile( projectDir ).getAbsolutePath() );
            }
            panel.fireChangeEvent();
        }
    }//GEN-LAST:event_awHomeButtonActionPerformed

    private void changeAWhomeChkBxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changeAWhomeChkBxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_changeAWhomeChkBxActionPerformed

    private void changeAWhomeChkBxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_changeAWhomeChkBxStateChanged
        // TODO add your handling code here:
        aribawebHomeTextField.setEnabled(changeAWhomeChkBx.isSelected());
        aribawebHomeTextField.setEditable(changeAWhomeChkBx.isSelected());
        awHomeButton.setEnabled(changeAWhomeChkBx.isSelected());
    }//GEN-LAST:event_changeAWhomeChkBxStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField aribawebHomeTextField;
    private javax.swing.JButton awHomeButton;
    private javax.swing.JButton browseButton;
    private javax.swing.JCheckBox changeAWhomeChkBx;
    private javax.swing.JLabel createdFolderLabel;
    private javax.swing.JTextField createdFolderTextField;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel projectLocationLabel;
    private javax.swing.JLabel projectLocationLabel1;
    private javax.swing.JTextField projectLocationTextField;
    private javax.swing.JLabel projectNameLabel;
    private javax.swing.JTextField projectNameTextField;
    // End of variables declaration//GEN-END:variables

    @Override
    public void addNotify()
    {
        super.addNotify();
        //same problem as in 31086, initial focus on Cancel button
        projectNameTextField.requestFocus();
    }

    boolean valid( WizardDescriptor wizardDescriptor )
    {

        if ( projectNameTextField.getText().length() == 0 )
        {
            // TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_ERROR_MESSAGE:
            wizardDescriptor.putProperty( "WizardPanel_errorMessage",
                    "Project Name is not a valid folder name." );
            return false; // Display name not specified
        }
        File f = FileUtil.normalizeFile( new File( projectLocationTextField.getText() ).getAbsoluteFile() );
        if ( !f.isDirectory() )
        {
            String message = "Project Folder is not a valid path.";
            wizardDescriptor.putProperty( "WizardPanel_errorMessage", message );
            return false;
        }
        final File destFolder = FileUtil.normalizeFile( new File( createdFolderTextField.getText() ).getAbsoluteFile() );

        File projLoc = destFolder;
        while ( projLoc != null && !projLoc.exists() )
        {
            projLoc = projLoc.getParentFile();
        }
        if ( projLoc == null || !projLoc.canWrite() )
        {
            wizardDescriptor.putProperty( "WizardPanel_errorMessage",
                    "Project Folder cannot be created." );
            return false;
        }

        if ( FileUtil.toFileObject( projLoc ) == null )
        {
            String message = "Project Folder is not a valid path.";
            wizardDescriptor.putProperty( "WizardPanel_errorMessage", message );
            return false;
        }

        File[] kids = destFolder.listFiles();
        if ( destFolder.exists() && kids != null && kids.length > 0 )
        {
            // Folder exists and is not empty
            wizardDescriptor.putProperty( "WizardPanel_errorMessage",
                    "Project Folder already exists and is not empty." );
            return false;
        }
        wizardDescriptor.putProperty( "WizardPanel_errorMessage", "" );
        return true;
    }

    void store( WizardDescriptor d )
    {
        String name = projectNameTextField.getText().trim();
        String folder = createdFolderTextField.getText().trim();
        String tempAwhome = aribawebHomeTextField.getText().trim();

        d.putProperty( "projdir", new File( folder ) );
        d.putProperty( "name", name );

        if ( tempAwhome != null && !tempAwhome.isEmpty() ){
            if ( !tempAwhome.equalsIgnoreCase(awHome) ) {
                NbPreferences.forModule(AribaWebOptionsPanel.class).put(
                        NBawConstants.AW_HOME, tempAwhome);
                awHome = tempAwhome;
            }
        }
        d.putProperty( NBawConstants.AW_HOME, awHome );
    }

    void read( WizardDescriptor settings )
    {
        File projectLocation = ( File ) settings.getProperty( "projdir" );
        if ( projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory() )
        {
            projectLocation = ProjectChooser.getProjectsFolder();
        }
        else
        {
            projectLocation = projectLocation.getParentFile();
        }
        this.projectLocationTextField.setText( projectLocation.getAbsolutePath() );

        String projectName = ( String ) settings.getProperty( "name" );
        if ( projectName == null )
        {
            projectName = "AribaWebProject";
        }
        this.projectNameTextField.setText( projectName );
        this.projectNameTextField.selectAll();
    }

    void validate( WizardDescriptor d ) throws WizardValidationException
    {
        // nothing to validate
    }

    // Implementation of DocumentListener --------------------------------------
    public void changedUpdate( DocumentEvent e )
    {
        updateTexts( e );
        if ( this.projectNameTextField.getDocument() == e.getDocument() )
        {
            firePropertyChange( PROP_PROJECT_NAME, null, this.projectNameTextField.getText() );
        }
    }

    public void insertUpdate( DocumentEvent e )
    {
        updateTexts( e );
        if ( this.projectNameTextField.getDocument() == e.getDocument() )
        {
            firePropertyChange( PROP_PROJECT_NAME, null, this.projectNameTextField.getText() );
        }
    }

    public void removeUpdate( DocumentEvent e )
    {
        updateTexts( e );
        if ( this.projectNameTextField.getDocument() == e.getDocument() )
        {
            firePropertyChange( PROP_PROJECT_NAME, null, this.projectNameTextField.getText() );
        }
    }

    /** Handles changes in the Project name and project directory, */
    private void updateTexts( DocumentEvent e )
    {

        Document doc = e.getDocument();

        if ( doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument() )
        {
            // Change in the project name

            String projectName = projectNameTextField.getText();
            String projectFolder = projectLocationTextField.getText();

            //if (projectFolder.trim().length() == 0 || projectFolder.equals(oldName)) {
            createdFolderTextField.setText( projectFolder + File.separatorChar + projectName );
            //}

        }
        panel.fireChangeEvent(); // Notify that the panel changed
    }
}
